from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from info import create_app, db
from info import models
# """
# manager.py的作用是入口程序
# """
from info.models import User

app = create_app("develop")
# 发呆发呆发
manager = Manager(app)
Migrate(app, db)
manager.add_command("mysql", MigrateCommand)

"""创建管理员对象"""
@manager.option('-n', '--name', dest='name')
@manager.option('-p', '--password', dest='password')
def create_super_user(name, password):
    if not all([name, password]):
        print("参数不足")
        return

    user = User()
    user.nick_name = name
    user.password = password
    user.mobile = name
    user.is_admin = True
    try:
        db.session.add(user)
        db.session.commit()
        print("创建成功")
    except Exception as e:
        db.session.rollback()


if __name__ == '__main__':
    manager.run()
